.386
.model flat,stdcall
option casemap:none

include windows.inc
include kernel32.inc
include user32.inc
include gdi32.inc
include msvcrt.inc
;include Common.inc

includelib kernel32.lib
includelib user32.lib
includelib gdi32.lib
includelib C:\masm32\lib\msvcrt.lib

IDI_HELLO_WIN		equ 	100
IDC_HELLO_BUTTON	equ 	1000

.data
g_hInst				dd		?
g_hMain				dd		?


.const
kHelloWin_Class		db		"HelloWin_Class", 0
kHelloWin_Title		db		"HelloWin Demo", 0
kButton_Class		db		"button", 0
kButton_Text		db		"OK", 0
kHelloWin_Text		db		"Win32 Assembly, Simple and powerful.", 0
kFormat				db	 	"\\vsjitdebugger.exe -p %d", 0

.code
AutoAttach PROC stdcall dwPid:DWORD
	local szPath[1024]:byte
	local szDebugger[1024]:byte
	local dwPathLen:DWORD
	invoke crt_memset, addr szPath, 0, SIZEOF szPath
	invoke crt_memset, addr szDebugger, 0, SIZEOF szDebugger
	mov dwPathLen, SIZEOF szPath
	invoke GetSystemDirectoryA, addr szPath, dwPathLen
	invoke crt_strcat, addr szPath, addr kFormat
	invoke crt_sprintf, addr szDebugger, addr szPath, dwPid
	invoke WinExec, addr szDebugger, SW_SHOW
	ret
AutoAttach ENDP

WinProc PROC uses ebx edi esi hWnd:DWORD, uMsg:DWORD, wParam:DWORD, lParam:DWORD
	local ps:PAINTSTRUCT
	local rect:RECT
	local hdc
	
	mov eax, uMsg
	.IF eax==WM_CREATE
		push hWnd
		pop g_hMain
		invoke CreateWindowEx,
					NULL,
					addr kButton_Class,
					addr kButton_Text,
					WS_CHILD or WS_VISIBLE,
					10, 10, 64, 24,
					hWnd,
					IDC_HELLO_BUTTON,
					g_hInst,
					NULL
	.ELSEIF eax==WM_PAINT
		invoke BeginPaint, hWnd, addr ps
		mov hdc, eax
		invoke GetClientRect, hWnd, addr rect
		invoke DrawText, hdc, addr kHelloWin_Text, -1, addr rect, DT_SINGLELINE or DT_CENTER or DT_VCENTER
		invoke EndPaint, hWnd, addr ps
	.ELSEIF eax==WM_CLOSE
		invoke DestroyWindow, g_hMain
		invoke PostQuitMessage, NULL
	.ELSE
		invoke DefWindowProc, hWnd, uMsg, wParam, lParam
		ret
	.ENDIF
	xor eax, eax
	ret
WinProc ENDP

WinMain PROC
	local wnd:WNDCLASSEX
	local msg:MSG
	local pid
	local icon:DWORD
	
	invoke GetCurrentProcessId
	mov pid, eax
	invoke AutoAttach, pid
	invoke MessageBox, NULL, NULL, NULL, MB_OK
	
	invoke GetModuleHandle, NULL
	mov g_hInst, eax
	invoke RtlZeroMemory, addr wnd, SIZEOF wnd
	invoke LoadCursor, NULL, IDC_ARROW
	mov wnd.hCursor, eax
	invoke LoadIcon, g_hInst, IDI_HELLO_WIN
	mov icon, eax
	push icon
	pop wnd.hIcon
	push icon
	pop wnd.hIconSm
	mov wnd.cbSize, SIZEOF WNDCLASSEX
	mov wnd.style, CS_HREDRAW or CS_VREDRAW
	mov wnd.lpfnWndProc, offset WinProc
	mov wnd.hbrBackground, COLOR_WINDOW+1
	mov wnd.lpszClassName, offset kHelloWin_Class
	invoke RegisterClassEx, addr wnd
	.IF eax==0
		ret
	.ENDIF
	
	invoke CreateWindowEx,
		WS_EX_CLIENTEDGE,
		addr kHelloWin_Class,
		addr kHelloWin_Title,
		WS_OVERLAPPEDWINDOW,
		100, 100, 600, 400,
		NULL,
		NULL,
		g_hInst,
		NULL
	.IF eax==0
		ret
	.ENDIF
	invoke ShowWindow, g_hMain, SW_SHOWNORMAL
	invoke UpdateWindow, g_hMain
	
	.WHILE TRUE
		invoke GetMessage, addr msg, NULL, 0, 0
		.BREAK .IF eax==0
		invoke TranslateMessage, addr msg
		invoke DispatchMessage, addr msg
	.ENDW
	ret
WinMain ENDP

start:
	call WinMain
	invoke ExitProcess, NULL
end start